package com.tensquare.base.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.base.pojo.City;
import com.tensquare.base.service.CityService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/city")
@CrossOrigin
public class CityController {
    @Autowired
    private CityService cityService;
    @PostMapping
    public Result save(@RequestBody City city){
        cityService.save(city);
        return new Result(true, StatusCode.OK,"ok");
    }
    //  GET/city 标签全部列表
    @GetMapping
    public Result findAll(){
        List<City>citys=cityService.findAll();
        return new Result(true,StatusCode.OK,"ok",citys);
    }
    // GET/city/{cityId}根据ID查询
    @GetMapping("/{cityId}")
    public Result findByCityId(@PathVariable String cityId) {
        City city=cityService.findById(cityId);
        return new Result(true,StatusCode.OK,"ok",city);
    }
    // PUT/city/{cityId}修改标签
    @PutMapping("/{cityId}")
    public Result findByCityId(@PathVariable String cityId,@RequestBody City city) {
        city.setId(cityId);
        cityService.updateByCityId(city);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/city/{cityId}根据ID删除
    @DeleteMapping("/{cityId}")
    public  Result deleteByCityId(@PathVariable String cityId){
        cityService.deleteByCityId(cityId);
        return new Result(true,StatusCode.OK,"ok");
    }
    /*
    * POST
    /city/search/{page}/{size}
    标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page<City> pageData=cityService.findByPage(map,page,size);
        PageResult<City> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * POST
    /city/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<City> list=cityService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
    }

}
